package com.hao.sort;

import java.util.Random;

/**
 * @Description: TODO 选择排序
 * @author: a'hao
 * @date: 2021/5/9
 */
public class SelectSort {

    public static void main(String[] args) {
        // 从0-10000中获取10000个元素的数组
        Integer[] arr = SortUtils.getRandomArr(500000, 500000);
        long start1 = System.currentTimeMillis();
        simpleSelectSort(arr);
        long end1 = System.currentTimeMillis();
        System.out.println("花费时间："+(end1-start1));
    }

    public static void simpleSelectSort(Integer[] arr){
        Integer index;
        Integer min;
        for (int i = 0; i < arr.length; i++) {
            min = arr[i];
            index = i;
            for (int j = i+1; j < arr.length; j++) {
                if (arr[j] < min){
                    min = arr[j];
                    index = j;
                }
            }
            if (i != index){
                exchange(arr,i,index);
            }
        }
    }

    public static void exchange(Integer[] arr,Integer index1,Integer index2){
        Integer temp = arr[index1];
        arr[index1] = arr[index2];
        arr[index2] = temp;
    }
    public static void printlnArr(Integer[] arr){
        for (int i = 0; i < arr.length; i++) {
            System.out.print(" " + arr[i]);
        }
        System.out.println();
    }
}
